/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package servercom;

import java.util.ArrayList;

/**
 *
 * @author Administrator
 */
public class TableDAO {

    ArrayList<RecordDAO> _lstRecord;
    ArrayList<Integer> _indexPK; // danh sách khác khóa chính
    ArrayList<FKDAO> _lstFKTo; //chứa khóa ngoại từ bảng khác trỏ đến
    String _name;
    private ArrayList<FKDAO> _lstFKFrom; //chứa khóa ngoại từ bảng này đến bảng khác
    //attribute
    ArrayList<AttributeDAO> _lstField;

    public ArrayList<Integer> getIndexPK() {
        return _indexPK;
    }

    public void setIndexPK(ArrayList<Integer> _indexPK) {
        this._indexPK = _indexPK;
    }

    public ArrayList<FKDAO> getLstFK() {
        return _lstFKTo;
    }

    public void setLstFK(ArrayList<FKDAO> _lstFK) {
        this._lstFKTo = _lstFK;
    }

    public ArrayList<RecordDAO> getLstRecord() {
        return _lstRecord;
    }

    public void setLstRecord(ArrayList<RecordDAO> _lstRecord) {
        this._lstRecord = _lstRecord;
    }

    public String getName() {
        return _name;
    }

    public void setName(String _name) {
        this._name = _name;
    }
    //method in table

    /**
     * @return the _lstFKFrom
     */
    public ArrayList<FKDAO> getLstFKFrom() {
        return _lstFKFrom;
    }

    /**
     * @param lstFKFrom the _lstFKFrom to set
     */
    public void setLstFKFrom(ArrayList<FKDAO> lstFKFrom) {
        this._lstFKFrom = lstFKFrom;
    }

    //method
    public TableDAO() {
        _lstRecord = new ArrayList<RecordDAO>();
        _indexPK = new ArrayList<Integer>();
        _lstFKTo = new ArrayList<FKDAO>();
        _lstFKFrom = new ArrayList<FKDAO>();
        _name = "";
        _lstField = new ArrayList<AttributeDAO>();
    }

    public TableDAO(String name) {
        _lstRecord = new ArrayList<RecordDAO>();
        _indexPK = new ArrayList<Integer>();
        _lstFKTo = new ArrayList<FKDAO>();
        _lstFKFrom = new ArrayList<FKDAO>();
        _name = name;
        _lstField = new ArrayList<AttributeDAO>();
    }

    //RecordDAO
    public boolean InsertRecord(RecordDAO record) {
        RecordDAO trecord;
        if (_indexPK.size() > 0) {
            for (int i = 0; i < _lstRecord.size(); i++) {
                trecord = _lstRecord.get(i);

                if (trecord.Compare(record, _indexPK) == true) {
                    return false;
                } else {
                    _lstRecord.add(record);
                }
            }
        } else {
            _lstRecord.add(record);
        }
        return true;
    }
    
    public boolean DeleteRecord(int indexRecord)
    {
        try
        {
            
        }
        catch(Exception e)
        {
            return false;
        }
        
        return true;
    }
    
    //kiểm tra xem các foreign trỏ tới có ảnh hưởng gì k?
    public boolean CheckForeignKeyTo(int indexRecord)
    {
        return true;
    }
    
    public int SearchRecord(int indexfield , CellDAO valuecompare)
    {
        try
        {
            if (_lstField.get(indexfield)._type.equalsIgnoreCase(valuecompare._value)) {
                return -1;
            }
            else
            {
                for (int i = 0; i < _lstRecord.size(); i++) {
                    CellDAO tcell = _lstRecord.get(i).getLstCell().get(indexfield);
                    if(tcell.Compare(valuecompare) == true)
                        return i;
                }
                return -1;
            }
        }
        catch(Exception e)
        {
            return -1;
        }

    }

    //Foreign Key
        //Foreign Key To
    
    public boolean InsertForeignKey( TableDAO tabledest , int indexto , int indexfrom)
    {
        try
        {
            FKDAO fkfrom = new FKDAO(this,tabledest,_lstField.get(indexto) , _lstField.get(indexfrom));
            _lstFKFrom.add(fkfrom);
            FKDAO fkto = new FKDAO(tabledest , this  , _lstField.get(indexfrom) ,_lstField.get(indexto));
            tabledest._lstFKTo.add(fkto);
        }
        catch(Exception e)
        {
            return false;
        }
        return true;
    }
    
    public boolean DeleteForeignKey(TableDAO tabledest , int indexto , int indexfroms)
    {        
        try
        {
            _lstFKFrom.remove(indexfroms);
            tabledest._lstFKTo.remove(indexto);
        }
        catch(Exception e)
        {
            return false;
        }
        return true;
    }

    
    
    public int SearchFKTo(AttributeDAO source)
    {
        for (int i = 0; i < _lstFKTo.size(); i++) {
               if (_lstFKTo.get(i)._fkDest._name.equalsIgnoreCase(source._name)) {
                   return i;
                
            }
        }
        return -1;
    }
    
    public int SearchFKFrom(AttributeDAO source)
    {
        for (int i = 0; i < _lstFKFrom.size(); i++) {
               if (_lstFKFrom.get(i)._fkSource._name.equalsIgnoreCase(source._name)) {
                   return i;
                
            }
        }
        return -1;
    }
    
    //Field
    public boolean InsertField(AttributeDAO field) {
        try {
            for (int i = 0; i < _lstField.size(); i++) {
                if (_lstField.get(i)._name.equalsIgnoreCase(field._name)) {
                    return false;
                }
            }
            _lstField.add(field);
        } catch (Exception e) {
            return false;
        }

        return true;
    }

    public boolean UpdateField(int index, AttributeDAO fieldnew) {
        if (fieldnew._type.equalsIgnoreCase("int")) {
            CellDAO cell;
            for (int i = 0; i < _lstRecord.size(); i++) {
                cell = _lstRecord.get(i).getLstCell().get(index);
                try {
                    Integer.parseInt(cell._value);
                } catch (Exception e) {
                    return false;
                }
            }
        }
        
        _lstField.set(index, fieldnew);
        return true;
    }
    
    public boolean  DeleteField(AttributeDAO fielddelete)//?????
    { 
        for (int i = 0; i < _lstFKTo.size(); i++) {
            
            if (_lstFKTo.get(i).getFkDest()._name.equalsIgnoreCase(fielddelete._name)) {
                return false;
            }
        }
        
        int index = SearchField(fielddelete);
        if(index != -1)
        {
            _lstField.remove(index);
        }
        
        return true;
    }
    
    public int SearchField(String name)
    {
        for (int i = 0; i < _lstField.size(); i++) {
            if (_lstField.get(i).getName().equalsIgnoreCase(name)) {
                return i;
            }
        }
        return -1;
    }
    
    public int SearchField(AttributeDAO attribute)
    {
        for (int i = 0; i < _lstField.size(); i++) {
            if (_lstField.get(i).getName().equalsIgnoreCase(attribute._name)) {
                return i;
            }
        }
        return -1;
    }
}
